/**
 * 
 */
package info.niwota.profile.web;

import info.niwota.commons.web.AjaxRegistry;
import info.niwota.commons.web.PageCursor;
import info.niwota.profile.Debug;
import info.niwota.webapp.web.PageContentProvider;

import java.io.File;

import org.json.JSONObject;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

/**
 * @author qiangli
 *
 */
public class ProfilePageContentProvider extends PageContentProvider {
	private static final String TAG = "ProfilePageContentProvider";

	@Override
	public boolean onCreate() {
		super.onCreate();
		//
		registerObjects();

		return true;
	}

	private void registerObjects() {
		final Context ctx = getContext();
		AjaxRegistry.getInstance().add(ProfileObject.NAME,
				new ProfileObject(ctx));
	}

	@Override
	protected void showMainPage(PageCursor c, Uri uri, int group, String user,
			String path) throws Exception {
		final String label = Label.LABEL_MY_PROFILE;
		checkAccess(uri, group, user, label);
		JSONObject data = new JSONObject();
		String peer = uri.getQueryParameter("peer");
		data.put("peer", NVL(peer, ""));
		handleAssetData(c, "main.tpl", TEXT_HTML, data);
	}

	@Override
	protected boolean showContent(PageCursor c, Uri uri, int group,
			String user, String path) throws Exception {
		if (Debug.DEBUG) {
			Log.d(TAG, "showContent path: " + path + " uri: " + uri);
		}
		
		// web pages
		if (path.startsWith("/web/") && path.endsWith(".htm")) {
			handleAsset(c, path.substring(1), "text/html");
			return true;
		}

		// only one label for now
		final String label = Label.LABEL_MY_PROFILE;

		checkAccess(uri, group, user, label);

		//
		final Context ctx = getContext();

		if (path.equals("/bin/photo")) {
			String size = uri.getQueryParameter("size");
			boolean data = Boolean.parseBoolean(uri.getQueryParameter("data"));
			
			final boolean thumb = (size != null && size.equals("thumbnail"));
			
			String peer = uri.getQueryParameter("peer");
			String jid = uri.getQueryParameter("jid");
			
			if (Debug.DEBUG) {
				Log.d(TAG, "######## peer: " + peer + " jid: " + jid + " data: "  + data);
			}
			
			File file = null;
			
			if (TextUtils.isEmpty(peer) || peer.equals(jid)) {
				boolean original = TextUtils.isEmpty(size) || size.equals("original");
				if (original) {
					file = Helper.getMyPhotoOriginal(ctx);
				} else {
					file = Helper.getMyPhoto(ctx, thumb);
				}
			} else {
				if (Debug.DEBUG) {
					Log.d(TAG, "######## downloading peer's photo: " + peer );
				}
				boolean nocache = Boolean.parseBoolean(uri.getQueryParameter("nocache"));
				
				file = Helper.getPeerPhotoFile(ctx, peer, thumb, nocache);
			}
			
			if (file == null) {
				showError(c, new Exception("Not found"));
				return true;
			}
			
			if (Debug.DEBUG) {
				Log.d(TAG, "######## file: " + file );
			}

			if (!data) {
				JSONObject jo = new JSONObject();
				jo.put("length", file.length());
				jo.put("lastModified", file.lastModified());
				showJson(c, jo.toString());
				return true;
			}
			
			showFile(c, file, "image/*");
			
			return true;
		}
		
		//
		throw new NotFoundException(uri.toString());

	}

}
